//-----------------------------------------------------------------------
// <copyright file="DeliveredOrderDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class DeliveredOrderDAO : DeliveredOrderDAOBase
	{
		public DeliveredOrderDAO()
		{
		}

		public virtual DeliveredOrderCollection GetDeliveredOrders(string productCode, string supplierCode, int owner)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spDeliveredOrdersGetListByProductCode");
                
				dbCommandWrapper.AddInParameter("@productCode", DbType.AnsiString, productCode);
				dbCommandWrapper.AddInParameter("@SupplierCode", DbType.AnsiString, supplierCode);
				dbCommandWrapper.AddInParameter("@owner", DbType.Int32, owner);
				
				DeliveredOrderCollection deliveredOrderCollection = new DeliveredOrderCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						DeliveredOrder deliveredOrder = CreateDeliveredOrderFromReader(reader);
						if (!reader.IsDBNull(reader.GetOrdinal("OfficeName")))
							deliveredOrder.OfficeName = (string)reader["OfficeName"];	
						deliveredOrderCollection.Add(deliveredOrder);
					}
					reader.Close();
				}
				
				return deliveredOrderCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetDeliveredOrdersException, ex);
			}
		}

		public virtual DeliveredOrderCollection GetDeliveredOrders(int bulkOrderID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spDeliveredOrdersGetListByBulkOrder");
                				
				dbCommandWrapper.AddInParameter("@bulkOrderID", DbType.Int32, bulkOrderID);
				
				DeliveredOrderCollection deliveredOrderCollection = new DeliveredOrderCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						DeliveredOrder deliveredOrder = CreateDeliveredOrderFromReader(reader);
						if (!reader.IsDBNull(reader.GetOrdinal("OfficeName")))
							deliveredOrder.OfficeName = (string)reader["OfficeName"];	
						if (!reader.IsDBNull(reader.GetOrdinal("ShippedDate")))
							deliveredOrder.ShippedDate = (DateTime)reader["ShippedDate"];	
						if (!reader.IsDBNull(reader.GetOrdinal("Req")))
							deliveredOrder.Req = (int)reader["Req"];	
						if (!reader.IsDBNull(reader.GetOrdinal("SalesOrder")))
							deliveredOrder.SalesOrder = (int)reader["SalesOrder"];
						
						deliveredOrderCollection.Add(deliveredOrder);
					}
					reader.Close();
				}
				
				return deliveredOrderCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetDeliveredOrdersException, ex);
			}
		}
	}
}
